package com.atguigu.meinian.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.meinian.mapper.TravelGroupMapper;
import com.atguigu.meinian.pojo.TravelGroup;
import com.atguigu.meinian.pojo.TravelItem;
import com.atguigu.meinian.service.TravelGroupService;
import com.atguigu.meinian.vo.PageResult;
import com.atguigu.meinian.vo.QueryPageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zbystart
 * @create 2021-06-21 14:41
 */
@Service(interfaceClass = TravelGroupService.class)
@Transactional
public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupMapper travelGroupMapper;

    @Override
    public PageResult list(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<TravelGroup> page = travelGroupMapper.selectByPageAndCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult(),page.getPageNum());
    }

    @Override
    public void add(TravelGroup travelGroup) {
        // 保存跟团游信息
        travelGroupMapper.insert(travelGroup);

        // 保存自游行信息
        setTravelItemIds(travelGroup.getId(),travelGroup.getTravelItems());
    }

    @Override
    public void update(TravelGroup travelGroup) {
        travelGroupMapper.updateById(travelGroup);

        // 先删除之前旧的
        Integer id = travelGroup.getId();
        travelGroupMapper.deleteTravelItemsByTravelGroupId(id);
        setTravelItemIds(id,travelGroup.getTravelItems());
    }

    @Override
    public Map<String, Object> queryById(Integer id) {
        TravelGroup travelGroup = travelGroupMapper.selectById(id);
        List<Integer> travelItemIds =  travelGroupMapper.selectTravelItemIds(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("travelGroup",travelGroup);
        map.put("travelItemIds",travelItemIds);
        return map;
    }

    @Override
    public void deleteById(Integer id) {
        // 先查询关联关系
        int count = travelGroupMapper.getTravelItemCount(id);
        if (count > 0) {
            // 有关联关系
            throw new RuntimeException("删除报团信息失败，请先删除关联信息");
        }
        // 没有关联关系
        travelGroupMapper.deleteTravelGroupById(id);
    }

    private void setTravelItemIds(Integer travelGroupId,List<Integer> travelItems) {
        // 保存自游行信息
        if (travelItems != null && travelItems.size() > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("travelGroupId",travelGroupId);
            map.put("list", travelItems);
            travelGroupMapper.insertTravelItems(map);
        }
    }

    // 前端移动开发
    @Override
    public List<TravelGroup> findTravelGroupsByIds(List<Integer> ids) {

        return null;
    }
}
